﻿using AuthorityCenter.Models;
using LedictCoreAuthorityLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;
using System.Web.Script.Serialization;

namespace AuthorityCenter.Controllers
{

    /// <summary>
    /// 
    /// </summary>
    [RoutePrefix("authority/v1")]
    public class AuthorityController : ApiController
    {
        /// <summary>
        /// 响应报文
        /// </summary>
        /// <param name="content">报文内容</param>
        /// <returns></returns>
        [NonAction]
        private HttpResponseMessage GetResponse(string content)
        {
            return new HttpResponseMessage()
            {
                Content = new StringContent(content, System.Text.Encoding.UTF8, "application/json"),
                StatusCode = HttpStatusCode.OK
            };
        }
        /// <summary>
        /// 获取硬件码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("gethcode")]
        public ResultObject Getcode([FromBody]dynamic data)
        {
            ResultObject robj = new ResultObject();
            robj.result = AuthorityModel.CheckServerRight().ToString();
            return robj;
        }
        /// <summary>
        /// 获取服务权限
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("checkserverright")]
        public ResultObject CheckServerRight()
        {
            ResultObject robj = new ResultObject();
            robj.result = AuthorityModel.CheckServerRight().ToString();
            return robj;
        }
        /// <summary>
        /// 获取系统权限
        /// </summary>
        /// <param name="courtCode">法院编号</param>
        /// <param name="sysCode">系统编号</param>
        /// <returns></returns>
        [HttpGet]
        [Route("checksystemright/{courtCode}/{sysCode}")]
        public ResultObject CheckSystemRight(string courtCode, string sysCode)
        {
            ResultObject robj = new ResultObject();
            robj.result = AuthorityModel.CheckSystemRight(courtCode, sysCode).ToString();
            return robj;
        }
        /// <summary>
        /// 获取法院权限
        /// </summary>
        /// <param name="courtCode">法院编号</param>
        /// <returns></returns>
        [HttpGet]
        [Route("checkcourtright/{courtCode}")]
        public ResultObject CheckCourtRight(string courtCode)
        {
            ResultObject robj = new ResultObject();
            robj.result = AuthorityModel.CheckCourtRight(courtCode).ToString();
            return robj;
        }
        /// <summary>
        /// 获取案由权限
        /// </summary>
        /// <param name="courtCode">法院编号</param>
        /// <param name="sysCode">系统编号</param>
        /// <param name="ccId">案由Id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("checkcauseright/{courtCode}/{sysCode}/{ccId}")]
        public HttpResponseMessage CheckCauseRight(string courtCode, string sysCode, string ccId)
        {
            string ret = AuthorityModel.CheckCauseRight(courtCode, sysCode, ccId);
            return GetResponse(ret);
        }
        /// <summary>
        /// 获取法院权限剩余时间
        /// </summary>
        /// <param name="courtCode">法院编号</param>
        /// <returns></returns>
        [HttpGet]
        [Route("getcourtexpiretime/{courtCode}")]
        public ResultObject GetCourtExpireTime(string courtCode)
        {
            ResultObject robj = new ResultObject();
            robj.result = AuthorityModel.GetExpireTime(courtCode, "").ToString();
            return robj;
        }
        /// <summary>
        /// 获取系统权限剩余时间
        /// </summary>
        /// <param name="courtCode">法院编号</param>
        /// <param name="sysCode">系统编号</param>
        /// <returns></returns>
        [HttpGet]
        [Route("getsystemexpiretime/{courtCode}/{sysCode}")]
        public ResultObject GetSystemExpireTime(string courtCode, string sysCode)
        {
            ResultObject robj = new ResultObject();
            robj.result = AuthorityModel.GetExpireTime(courtCode, sysCode).ToString();
            return robj;
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="obj">请求对象</param>
        /// <returns></returns>
        [HttpPost]
        [Route("decryp")]
        public ResultObject Decryp([FromBody] PostObject obj)
        {
            ResultObject robj = new ResultObject();
            robj.result = AuthorityModel.Decryp(obj.srcStr);
            return robj;
        }

        /// <summary>
        /// 获取授权版本
        /// </summary>
        /// <param name="obj">请求对象</param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetFyCourtRight")]
        public PiecewiseResults GetFyCourtRight(PostFyCourtObject obj)
        {
            return AuthorityModel.GetFyCourtRightResult(obj);
        }
        /// <summary>
        /// 获取授权版本（无硬件码）
        /// </summary>
        /// <param name="obj">请求对象</param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetFyCourtRightNoHcode")]
        public PiecewiseResults GetFyCourtRightNoHcode(PostFyCourtObject obj)
        {
            obj.Hcode = null;
            return AuthorityModel.GetFyCourtRightResult(obj);
        }
    }
}
